#ifndef _LOADER_
#define _LOADER_

#include <string>
#include <vector>

#include <istream>

#include "Interpreter.h"

class Loader
{
public:
    Loader(Interpreter& i);
    void LoadModule(const std::string& filename);

private:
    
    std::vector<char> raw;

    StringPool& pool;
    char *& bytecode;
    Classes& classes;
    int& entry_point;
    UndefinedFunctions& undefs;
    AddrToNameMap& addr_to_name;

    void ProcessInput();
    void ProcessStringPool(int start, int end);
    void ProcessFunctions(int start, int end);
    void ProcessClasses(int start, int end);

    template <typename T>
    inline T GetRawAt(int i)
    {
        return *reinterpret_cast<T *>(&raw[i]);
    }

    template <typename T>
    inline T GetRawAtInc(int& i)
    {
        T ret = *reinterpret_cast<T *>(&raw[i]);
        i += sizeof(T);
        return ret;
    }
};

#endif
